//初始化列表数组
var List = [];
var startDate = '';
var endDate = '';
var editUserId = 0;

//分页查询
var limit = 5;// 每页多少条
var currPage = 1;// 当前页
var allPage = 1;// 总页数
var count = 0;// 默认共多少条数据

function doAdd(){
    $('#doAdd').modal({
        show:true,//展示出来
        moveable:true,//可移动
        rememberPos:true,//记住移动位置
        backdrop:'static'//背景遮罩
    });
}
function confirm(){
    $('#doAdd').modal('hide', 'fit')
}





// getlist();
function getlist(){
	$.ajax({
		url : ajax_host +'selectGK.act',
		dataType:'JSON',
		
		success:function(resp){
			if(resp.success){
				List = resp.list;
				console.log("列表："+List);
				renderTable(resp.list);
			}
		}
	})
}
getUserlist();
function getUserlist(){
	console.log(startDate);
	console.log(endDate);

	
	
	$.ajax({
		url:ajax_host+'getUserlist.act',
		dataType : 'JSON',
		data:{
			name : val('name'),	
			sex : $('#sex').val(),
			start : startDate,
			end: endDate,
			startpage : currPage,
			limitpage : limit
		},
		success:function(resp){
			
			if(resp.success){
				List = resp.list;
				console.log("查询后列表："+List);
				renderTable(resp.list);
				// 总页数=记录数/每页记录
				count = resp.count;
				console.log('总记录数:'+count)
				allPage=count%limit==0?(count/limit):(parseInt(count/limit+1));
				console.log('总页数:'+allPage);
				$("#pager").html(currPage+"/"+allPage);
			}else{
				layer.alert(resp.errmsg);
			}
        }
	})
}

function renderTable(arr){
	var str = `<tr>
		<th>用户名</th>
		<th>性别</th>
		<th>状态</th>
		<th>注册时间</th>
		<th>余额</th>
		<th>操作</th>
	</tr>`;
	for(var i = 0;i<arr.length;i++){
		var a = arr[i];
		str += 
		   `<tr>
			<td>${a.uName}</td>
			<td>${a.uSex ==1? '男':'女'}</td>
			
			<td>`;
			if(a.uState == 0) str +='启用';
			if(a.uState == 1) str +='禁用';
			str +=`</td>
			<td>${a.uCtime}</td>
			
			<td>${a.uMoney}</td>
			
			<td>
			<a href = "###" onclick="doUpdate(${i},${a.uId})">修改状态</a>
			<a href = "###" onclick="del(${i})">删除</a>
			<a href = "###" onclick="newpassword(${i})">重置密码</a>
			</td>
		</tr>`;
		
	}
	html('table',str);
}

function getstartDate(){
	startDate = document.getElementById('start').value;
	
	
}
function getendDate(){
	endDate = document.getElementById('end').value;
	
}


function doSend(){
	name = val('name');
	sex = $('#sex').val();
	getUserlist();
}



function doUpdate(index,uId){
	console.log("遮罩获取列表："+List);
	
	console.log("获取的uid:"+uId);
	
    $('#doUpdate').modal({
        show:true,//展示出来
        moveable:true,//可移动
        rememberPos:true,//记住移动位置
		backdrop:'static'//背景遮罩
	});
	var user = List[index];
	editUserId = uId;
	console.log('user的值为'+user);
	console.log('edit的值为'+editUserId);
	//拿到单选框对象
	var radios = document.getElementsByName('status1');
	for(var i = 0; i<radios.length; i++){
		if(radios[i].value == user.state){//判断value和我们的值是否相等
			
			radios[i].checked = true;		
			
		}
	}
}

function confirm2(){
	
	// $('#doUpdate').modal('hide', 'fit');
	$.ajax({
		url:ajax_host+'userEdit.act',
		type : 'post',
		dataType:'JSON',
		data:{
			
			status : val('status1','radio'),
			uid : editUserId
		},
		success:function(resp){
			
			if(resp.success){
				//关闭遮罩
				$('#doUpdate').modal('hide', 'fit');
				layer.alert('修改成功');
				getUserlist();
			}else{
				$('#doUpdate').modal('hide', 'fit');
				layer.alert(resp.errmsg);
			}
		}
})

}

//给删除按钮挂事件
function del(index){
	console.log('列表删除'); 
	layer.confirm("确定删除该用户吗？", {
		btn: ['确定','取消'] //按钮
	   ,skin: 'layui-layer-lan'
	   ,closeBtn: 0
	   ,anim: 3 //动画类型
   },function(){

	var user = List[index];
	//空指针的判断
	console.log('判断'+user)
	if(!user || user.uId <=0){
		return;
	}
	$.ajax({
		url:ajax_host+'userDel.act',
		type:'post',
		dataType:'JSON',
		data:{
			uid : user.uId
		},
	success:function(resp){
		if(resp.success){
			//删除数组的元素
			layer.alert('删除成功');
			
			getUserlist();
			
		}else{
			layer.alert(resp.errmsg);
		}
		
	}
	})


   });
		
	
}

function newpassword(index){
	console.log('重置密码');
	layer.confirm("确定重置该用户密码吗？", {
		btn: ['确定','取消'] //按钮
	   ,skin: 'layui-layer-lan'
	   ,closeBtn: 0
	   ,anim: 3 //动画类型
   },function(){

	var user = List[index];
	//空指针的判断
	console.log('判断'+user)
	if(!user || user.uId <=0){
		console.log('空指针了')
		return;
	}
	$.ajax({
		url:ajax_host+'userNewPwd.act',
		type : 'post',
		dataType:'JSON',
		data:{
			uid : user.uId
		},
		success:function(resp){
			
			if(resp.success){
				
				layer.alert('重置成功,密码为a123456');
				getUserlist();
			}else{
				layer.alert(resp.errmsg);
			}
		}
	})


   });
	
}
function pagenext() {
	if (currPage + 1 > allPage) {
		layer.alert("当前已经是最后一页");
		return;
	}
	
	currPage = currPage + 1;
	getUserlist();
};

function pageback() {
	if (currPage - 1 == 0) {
		layer.alert("当前已经是第一页");
		return;
	}
	
	currPage = currPage - 1;
	getUserlist();
};